package com.lagou.edu.authority.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.authority.dto.AllocateRoleResourceDTO;
import com.lagou.edu.authority.dto.ResourceCategoryDTO;
import com.lagou.edu.authority.dto.ResourceDTO;
import com.lagou.edu.authority.entity.Resource;
import com.lagou.edu.authority.entity.ResourceCategory;
import com.lagou.edu.authority.param.ResourceQueryParam;
import com.lagou.edu.authority.service.IResourceCategoryService;
import com.lagou.edu.authority.service.IResourceService;
import com.lagou.edu.common.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源controller
 *
 * @author lm
 * @since 2021/11/5 11:00
 */
@Slf4j
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    IResourceService resourceService;

    @Autowired
    IResourceCategoryService resourceCategoryService;

    @PostMapping("/saveOrUpdateResource")
    public Boolean saveOrUpdateResource(@RequestBody ResourceDTO resourceDTO) {
        Resource resource = ConvertUtils.convert(resourceDTO, Resource.class);
        return resourceService.saveOrUpdate(resource);
    }

    @GetMapping("/{id}")
    public Boolean deleteById(@PathVariable("id") Integer id) {
        return resourceService.removeById(id);
    }

    /**
     * 按条件分页查询资源
     *
     * @param resourceQueryParam
     * @return
     */
    @PostMapping("/getResourcePages")
    Page<ResourceDTO> getResourcePages(@RequestBody ResourceQueryParam resourceQueryParam) {
        Page<Resource> resourcePages = resourceService.getResourcePages(resourceQueryParam);
        Page<ResourceDTO> resourceDTOPages = new Page<>();
        BeanUtils.copyProperties(resourcePages, resourceDTOPages);
        if (CollectionUtils.isNotEmpty(resourcePages.getRecords())) {
            List<ResourceDTO> records = resourcePages.getRecords().stream()
                    .map(role -> ConvertUtils.convert(role, ResourceDTO.class)).collect(Collectors.toList());
            resourceDTOPages.setRecords(records);
        }
        return resourceDTOPages;
    }

    @GetMapping("/getAllResource")
    List<ResourceDTO> getAllResource() {
        return ConvertUtils.convertList(resourceService.list(), ResourceDTO.class);
    }

    @PostMapping("/allocateRoleResources")
    Boolean allocateRoleResources(AllocateRoleResourceDTO allocateRoleResourceDTO) {
        log.info("Allocate role resources, params:{}", allocateRoleResourceDTO);
        return resourceService.allocateRoleResources(allocateRoleResourceDTO);
    }

    /**
     * 查询所有资源分类列表
     *
     * @return
     */
    @GetMapping("/category/getAll")
    public List<ResourceCategoryDTO> getAllCategories() {
        return ConvertUtils.convertList(resourceCategoryService.list(), ResourceCategoryDTO.class);
    }

    @GetMapping("/category/getById")
    public ResourceDTO getResourceDTOById(@RequestParam("id") Integer id) {
        return ConvertUtils.convert(resourceCategoryService.getById(id), ResourceDTO.class);
    }

    @PostMapping("/category/saveOrUpdateResourceCategory")
    public Boolean saveOrUpdateResourceCategory(@RequestBody ResourceCategoryDTO resourceCategoryDTO) {
        ResourceCategory resourceCategory = ConvertUtils.convert(resourceCategoryDTO, ResourceCategory.class);
        return resourceCategoryService.save(resourceCategory);
    }
}
